﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Data.SqlClient;

namespace Demo_HHT_EARTH
{
    public partial class DLflight2 : Form
    {
        string fl_no = string.Empty;
        DataSet DatafldInfo = new DataSet();
        DataSet data_emp = new DataSet();
        DataSet data_eq = new DataSet();
        SQLiteConnection cn2 = new SQLiteConnection();


        
        public DLflight2(DataSet data, DataSet data_em, DataSet data_eq, string flight_no)//(DataSet data,DataSet data_em ,DataSet data_eq,string flight_no)
        {
            InitializeComponent();
            fl_no = flight_no;
            label5.Text = fl_no;
            DatafldInfo = data;
            data_emp = data_em;
            //
            if (DatafldInfo == null || data_emp == null || data_eq == null) //(DatafldInfo==null || data_emp == null|| data_eq == null)
            {
                label3.Visible = true;
                label3.Text = "Download Data Error";
                //MessageBox.Show("Download Data Error");
            }
            else
            {


                InsertDataFlightEmp(data_emp);
                InsertDataFlightEq(data_eq);
                if (progressBar1.Maximum.ToString().Equals(progressBar1.Value.ToString()))
                {
                    label3.Visible = true;
                    label3.Text = "Downloand Successful";
                }
            }
          
        }

        private void InsertDataFlightEq(DataSet data_eq)
        {
            try
            {
                cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
            }
            catch (Exception)
            {
                label3.Visible = true;
                label3.Text = "notconect SQLite";
               // MessageBox.Show("notconect SQLite");
            }
            DataTable dt = new DataTable();
            dt = data_eq.Tables[0];
            if (dt == null)
            {
                label3.Visible = true;
                label3.Text = "no data";
                //MessageBox.Show("no data");
            }
            else
            {

                progressBar1.Maximum = dt.Rows.Count-1;

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string datenow = String.Format("{0:M/d/yyyy}", DateTime.Now);
                    Guid flig_em_id = Guid.NewGuid();
                    strconinsert = "INSERT INTO [t_earth_flight_equipment] (earth_flight_equipment_id,flight_id,flight_equipment_id,equipment_code,status,check_in_flag,check_in_datetime,description,create_date,create_by) VALUES('"
                        + flig_em_id.ToString()
                        + "','" + Demo_HHT_EARTH.DataGetflightInfo.Gufld_id
                        + "','" + dt.Rows[i]["flight_equipment_id"].ToString()
                        + "','" + dt.Rows[i]["equipment_code"].ToString()
                        + "','" + dt.Rows[i]["status"].ToString()
                        + "','" + dt.Rows[i]["check_in_flag"].ToString()
                        + "','" + datenow
                        + "','" + dt.Rows[i]["equipment_description"].ToString()
                        + "','" + datenow
                        + "','" + Demo_HHT_EARTH.GobalVariableEARTH.UserID
                        + "')";


                    SQLiteCommand command = new SQLiteCommand(strconinsert, cn2);
                    cn2.Open();
                    command.Connection = cn2;

                    command.ExecuteNonQuery();
                    cn2.Close();
                    progressBar1.Value = i;
                   
                }
                string strcomm = "SELECT * FROM [t_earth_flight_equipment]";
                SQLiteCommand COMM = new SQLiteCommand(strcomm, cn2);
                SQLiteDataAdapter Lda = new SQLiteDataAdapter(COMM);
                Lds = new DataSet();
                cn2.Open();
                cn2.Close();
                Lda.Fill(Lds, "L_emp");
                //dataGrid1.DataSource = Lds.Tables[0];
            }
        }

        private void InsertDataFlightEmp(DataSet data_emp)
        {

            try
            {
                cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
            }
            catch (Exception)
            {
                label3.Visible = true;
                label3.Text = "no data";

                //MessageBox.Show("notconect SQLite");
            }
            DataTable dt = new DataTable();
            dt = data_emp.Tables[0];
            if (dt == null)
            {
                label3.Visible = true;
                label3.Text = "no data";
                //MessageBox.Show("not OK");
            }
            else
            {

                progressBar1.Maximum = dt.Rows.Count-1;

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string datenow = String.Format("{0:M/d/yyyy}", DateTime.Now);
                    Guid flig_em_id = Guid.NewGuid();
                    strconinsert = "INSERT INTO [t_earth_flight_employee] (earth_flight_employee_id,flight_id,flight_employee_id,employee_code,employee_group,status,check_in_flag,check_in_datetime,employee_display_name,create_date,create_by) VALUES('"
                        + flig_em_id.ToString()
                        + "','" + Demo_HHT_EARTH.DataGetflightInfo.Gufld_id
                        + "','" + dt.Rows[i]["flight_employee_id"].ToString()
                        + "','" + dt.Rows[i]["employee_code"].ToString()
                        + "','" + dt.Rows[i]["employee_group"].ToString()
                        + "','" + dt.Rows[i]["status"].ToString()
                        + "','" + dt.Rows[i]["check_in_flag"].ToString()
                        + "','" + datenow
                        + "','" + dt.Rows[i]["employee_display_name"].ToString()
                        + "','" + datenow
                        + "','" + Demo_HHT_EARTH.GobalVariableEARTH.UserID
                        + "')";


                    SQLiteCommand command = new SQLiteCommand(strconinsert, cn2);
                    cn2.Open();
                    command.Connection = cn2;

                    command.ExecuteNonQuery();
                    cn2.Close();
                    progressBar1.Value = i;
                    //progressBar1.Refresh();

                    //checkrow = i;
                }
                string strcomm = "SELECT * FROM [t_earth_flight_employee]";
                SQLiteCommand COMM = new SQLiteCommand(strcomm, cn2);
                SQLiteDataAdapter Lda = new SQLiteDataAdapter(COMM);
                Lds = new DataSet();
                cn2.Open();
                cn2.Close();
                Lda.Fill(Lds, "L_emp");
              
            }
        }

        //private void InsertDataFlightInfo(DataSet DataEmp)
        //{
        //    DataSet datafldInfo = new DataSet();
        //    datafldInfo = DataEmp;
        //    if (datafldInfo.Tables[0].Rows.Count > 0)
        //    {
        //        //dataGrid1.DataSource = datafldInfo.Tables[0]; 
        //        //MessageBox.Show(datafldInfo.Tables[0].Rows[0]["flight_date"].ToString());
        //        //string sfltD = string.Format("{0:dd MMM yyyy}", datafldInfo.Tables[0].Rows[0]["flight_date"]);
        //        //Demo_HHT_EARTH.DataGetflightInfo.fld_date = sfltD;
        //        //Demo_HHT_EARTH.DataGetflightInfo.fld_number = datafldInfo.Tables[0].Rows[0]["flight_number"].ToString();
        //        //Demo_HHT_EARTH.DataGetflightInfo.start_std = datafldInfo.Tables[0].Rows[0]["scheduled_time_departure"].ToString();
        //        //Demo_HHT_EARTH.DataGetflightInfo.routing = datafldInfo.Tables[0].Rows[0]["routing"].ToString();
        //        //Demo_HHT_EARTH.DataGetflightInfo.Parking_bay = datafldInfo.Tables[0].Rows[0]["bay"].ToString();
        //       // DisplayFlightInfo();
                
        //    }
        //    else
        //    {
        //        label3.Visible = true;
        //        label3.Text = "Can not Getflight Information";
        //    }
        //}

        //private void DisplayFlightInfo()
        //{
        //    label3.Visible = true;

        //    label3.Text = "Date : " + Demo_HHT_EARTH.DataGetflightInfo.fld_date + '\n'
        //        + "Flight Number : " + Demo_HHT_EARTH.DataGetflightInfo.fld_number + '\n'
        //        + "STARTSTD : " + Demo_HHT_EARTH.DataGetflightInfo.start_std + '\n'
        //        + "Routing : " + Demo_HHT_EARTH.DataGetflightInfo.routing + '\n'
        //        + "Parking Bay : " + Demo_HHT_EARTH.DataGetflightInfo.Parking_bay ;
        //}

        //private void InserDataGetflight(DataSet DataEmp)
        //{
        //    try
        //    {
        //        cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
        //    }
        //    catch (Exception)
        //    {

        //        MessageBox.Show("notconect SQLite");
        //    }
        //    DataTable dt = new DataTable() ;
        //    dt = DataEmp.Tables[0];
        //   // dataGrid1.DataSource = dt;
        //    if (dt == null)
        //    {
        //        MessageBox.Show("not OK");
        //    }
        //    else
        //    {
        //        dt.Columns.Add("flight_number");
        //         progressBar1.Maximum = dt.Rows.Count;

        //         for (int i = 0; i < dt.Rows.Count; i++)
        //         {
        //             //strconinsert = "INSERT INTO [t_earth_flight] (fid,flight_date,flight_number,carrier,carrier_name,scheduled_time_arrival,scheduled_time_departure,routing,bay,flight_status,user_id) VALUES('"
        //             //    + dt.Rows[i]["fid"].ToString()
        //             //    + "','" + dt.Rows[i]["flight_date"].ToString()
        //             //    + "','" + dt.Rows[i]["flight_number"].ToString()
        //             //    + "','" + dt.Rows[i]["carrier"].ToString()
        //             //    + "','" + dt.Rows[i]["carrier_name"].ToString()
        //             //    + "','" + dt.Rows[i]["scheduled_time_arrival"].ToString()
        //             //    + "','" + dt.Rows[i]["scheduled_time_departure"].ToString()
        //             //    + "','" + dt.Rows[i]["routing"].ToString()
        //             //    + "','" + dt.Rows[i]["bay"].ToString()
        //             //    + "','" + dt.Rows[i]["flight_status"].ToString()
        //             //    + "','" + dt.Rows[i]["user_id"].ToString()
        //             //    + "')";
        //            // strconinsert = "INSERT INTO [t_earth_flight] (flight_number) VALUES ('"+dt.Rows[i]["flight_number"].ToString()+ "')";
        //             SQLiteCommand command = new SQLiteCommand(strconinsert, cn2);
        //             cn2.Open();
        //             command.Connection = cn2;

        //             command.ExecuteNonQuery();
        //             cn2.Close();
        //             progressBar1.Value = i;
        //             //progressBar1.Refresh();

        //             checkrow = i;
                     
        //         }
        //         if (dt.Rows.Count>0)
        //         {
        //             label3.Visible = true;
        //             label3.Text = "Dowload Data is OK ";
        //         }

        //    }
        //}
       
        DataSet Lds = new DataSet();
        string strconinsert = string.Empty; 
       



        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
            Demo_HHT_EARTH.DLflight dl = new DLflight();
            dl.Show();
          
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            this.Close();
            DEMO_TEMP1.MainMenu dm = new DEMO_TEMP1.MainMenu(true,2);
            dm.Show();
            
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {

        }

    }
}